import os
import sqlobject
from sqlobject.sqlite import builder

"""Each objects definition in the database"""


store = "demo.db"
conn = builder()(store)

class Link(sqlobject.SQLObject):
   _connection = conn
   direction = sqlobject.StringCol()
   exit = sqlobject.ForeignKey('Room')
   destination = sqlobject.ForeignKey('Room')

   def describe(self):
      return self.direction

class Room(sqlobject.SQLObject):
   _connection = conn
   description = sqlobject.StringCol()
   exits = sqlobject.MultipleJoin('Link',joinColumn='exit_id')
   players = sqlobject.MultipleJoin('Player',joinColumn='currentroom_id')   #Is this really needed?

   def describe(self):
      string = self.description+"\nThere are exits to the "
      string+= ",".join([exit.describe() for exit in self.exits])
      string+="\n"
      for player in self.players:
         string+=player.name +" is listed in the db here\n"
      return string

class Player(sqlobject.SQLObject):
   _connection = conn
   name = sqlobject.StringCol()
   currentroom = sqlobject.ForeignKey('Room')


"""r4 - r3 - r2
         | 
        r1"""

if __name__ == "__main__":
   if not os.path.exists(store):
      Room.createTable()
      Player.createTable()
      Link.createTable()

      r1 = Room(description="This is room one")
      r2 = Room(description="This is room two")
      r3 = Room(description="This is room three")
      r4 = Room(description="This is room four")

      Link(direction="north",exit=r1,destination=r3)
      Link(direction="south",exit=r3,destination=r1)

      Link(direction="west",exit=r3,destination=r4)
      Link(direction="east",exit=r4,destination=r3)

      Link(direction="east",exit=r3,destination=r2)
      Link(direction="west",exit=r2,destination=r3)

      Player(name="biffabacon",currentroom=1)
      Player(name="pabacon",currentroom=2)
      Player(name="mabacon",currentroom=2)
